package com.demo.service.impl;


import com.demo.entity.enums.PageSize;
import com.demo.entity.po.WebCarousel;
import com.demo.entity.query.SimplePage;
import com.demo.entity.query.WebCarouselQuery;
import com.demo.entity.vo.PaginationResultVO;
import com.demo.mappers.WebCarouselMapper;
import com.demo.service.WebCarouselService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:web轮播serviceImpl
 * @Date:2025-03-04
 * @author：author
*/
@Service("webCarouselService")
public class WebCarouselServiceImpl implements WebCarouselService{

	@Resource
	private WebCarouselMapper<WebCarousel,WebCarouselQuery> webCarouselMapper;

	/**
	 * 根据条件查询列表
	 */
	public List<WebCarousel> findListByParam(WebCarouselQuery query) { 
		return this.webCarouselMapper.selectList(query);
	}

	/**
	 * 根据条件查询数量
	 */
	public Integer findCountByParam(WebCarouselQuery query){
		return this.webCarouselMapper.selectCount(query);
	}

	/**
	 * 分页查询
	 */
	public PaginationResultVO<WebCarousel> findListByPage(WebCarouselQuery  query) { 
		int count = this.findCountByParam(query);
		int pageSize = query.getPageSize()==null?PageSize.SIZE15.getSize():query.getPageSize();
		SimplePage page = new SimplePage(query.getPageNo(),count,pageSize);
		query.setSimplePage(page);
		List<WebCarousel> list = this.findListByParam(query);
		return new PaginationResultVO(count,page.getPageSize(),page.getPageNo(),page.getPageTotal(),list);
	}

	/**
	 * 新增
	 */
	public Integer add(WebCarousel bean) { 
		return this.webCarouselMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	public Integer addBatch(List<WebCarousel> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.webCarouselMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	public Integer addOrUpdateBatch(List<WebCarousel> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.webCarouselMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 根据CarouselId查询
	 */
	public WebCarousel getWebCarouselByCarouselId(Integer carouselId) { 
		return this.webCarouselMapper.selectByCarouselId(carouselId);
	}

	/**
	 * 根据CarouselId更新
	 */
	public Integer updateWebCarouselByCarouselId(WebCarousel bean, Integer carouselId) { 
		return this.webCarouselMapper.updateByCarouselId(bean,carouselId);
	}

	/**
	 * 根据CarouselId删除
	 */
	public Integer deleteWebCarouselByCarouselId(Integer carouselId) { 
		return this.webCarouselMapper.deleteByCarouselId(carouselId);
	}


    /**
     * 保存轮播图信息
	 * @param bean
	 */
	@Override
	public void saveCarousel(WebCarousel bean) {
		if (bean.getCarouselId() == null) {
			WebCarouselQuery appCarouselQuery = new WebCarouselQuery();
			Integer count = webCarouselMapper.selectCount(appCarouselQuery);
			bean.setSort(count + 1);
			this.webCarouselMapper.insert(bean);
		} else {
			this.webCarouselMapper.updateByCarouselId(bean, bean.getCarouselId());
		}
	}

	/**
	 * 修改排序
	 * @param carouselIds
	 */
	@Override
	public void changeSort(String carouselIds) {
		String[] carouselIdArray = carouselIds.split(",");
		Integer index = 1;
		for (String carouselIdStr : carouselIdArray) {
			Integer carouselId = Integer.parseInt(carouselIdStr);
			WebCarousel appCarousel = new WebCarousel();
			appCarousel.setSort(index);
			webCarouselMapper.updateByCarouselId(appCarousel, carouselId);
			index++;
		}
	}
}